--------
-- ***********************************************
-- * Nightmare
-- ***********************************************
--------
function special_nightmare()

	Attack.act_aseq(0, "castmagic")
	local dmgt = Attack.aseq_time(0, "x")
	local count = Attack.act_size(0)

	local n = Attack.cell_count()
	for j=0,n-1 do

		local i = Attack.cell_get(j)
		local duration = Attack.get_custom_param("duration")
		local dmg_min,dmg_max = text_range_dec(Attack.get_custom_param("damage"))
		local typedmg=Attack.get_custom_param("typedmg")

		if Attack.act_enemy(i)
		and not Attack.act_feature(i,"plant,golem,mech") then

			if Attack.act_feature(i,"holy") then

				Attack.atk_set_damage(typedmg,dmg_min*2,dmg_max*2)

			elseif Attack.act_feature(i,"undead")
			or Attack.act_name(i)=="griffin_spirit" then

				Attack.atk_set_damage(typedmg,dmg_min/200,dmg_max/200)
			else

				Attack.atk_set_damage(typedmg,dmg_min,dmg_max)
			end

			Attack.atom_spawn(i, dmgt, "effect_nightmare", Attack.angleto(i))
			Attack.atom_spawn(i, dmgt, "effect_nightmare2", Attack.angleto(i))
			common_cell_apply_damage(i,dmgt+.5)

			if not (Attack.act_feature(i,"undead,mind_immunitet,pawn,boss")
			or Attack.act_name(i)=="griffin_spirit") then

				if (Game.CurLocRand(1,100) <80
				and Attack.act_feature(i,"holy")) then

					effect_sleep_attack(i,dmgt+.5,duration+1,false)

					if Attack.act_size(i)>1 then

						Attack.log(0.001,"add_blog_sleep_2", "targets", "   "..blog_side_unit(i,0))
					else
						Attack.log(0.001,"add_blog_sleep_1", "target", "   "..blog_side_unit(i,0))
					end

				elseif not Attack.act_feature(i,"holy") then

					if (Game.CurLocRand(1,100) <10 and Attack.act_level(i)==5)
					or (Game.CurLocRand(1,100) <20 and Attack.act_level(i)==4) then

						effect_sleep_attack(i,dmgt+.5,duration,false)

						if Attack.act_size(i)>1 then

							Attack.log(0.001,"add_blog_sleep_2", "targets", "   "..blog_side_unit(i,0))
						else
							Attack.log(0.001,"add_blog_sleep_1", "target", "   "..blog_side_unit(i,0))
						end

					elseif Game.CurLocRand(1,100) <40 and Attack.act_level(i)<4 then

						effect_sleep_attack(i,dmgt+.5,duration+1,false)

						if Attack.act_size(i)>1 then
							Attack.log(0.001,"add_blog_sleep_2", "targets", "   "..blog_side_unit(i,0))
						else
							Attack.log(0.001,"add_blog_sleep_1", "target", "   "..blog_side_unit(i,0))
						end
					end
				end
			end
  		end
	end
	return true
end

function special_fire_disasters()
	local target = Attack.get_target()
	Attack.aseq_rotate(0,target)
	Attack.act_aseq(0, "victory")
	local atom, b
	local t,ts = Attack.aseq_time(0)
	Attack.cam_shake(ts, "quake_small.envlp")
	for i=1,6 do
		atom = "magic_fireground_"..math.random(1,3);
		b = Attack.atom_spawn(target, t, atom, Game.Dir2Ang(math.random(0,5)))
		t = Attack.aseq_time(b, "x") + i*0.25
	end
	common_cell_apply_damage(target, t-1.0)
    for j=0,5 do
	local tgt=Attack.cell_adjacent(target,j)
		if tgt~=nil then
			for i=1,5 do
				atom = "magic_fireground_"..math.random(1,3);
				b = Attack.atom_spawn(tgt, t, atom, Game.Dir2Ang(math.random(0,5)))
				t = Attack.aseq_time(b, "x") + i*0.5
				if j==1 and i<4 then Attack.cam_shake(t, "quake_small.envlp") end
			end
			if tgt~=nil and Attack.cell_present(tgt) and Attack.act_applicable(tgt) then common_cell_apply_damage(tgt, t-2.5) end
		end
	end
	return true
end
